package com.example.controller;

import com.example.common.Result;
import com.example.common.ResultCode;
import com.example.model.dto.ContactFormDTO;
import com.example.model.entity.ContactConfig;
import com.example.model.entity.ContactForm;
import com.example.service.ContactConfigService;
import com.example.service.ContactFormService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 联系相关控制器
 */
@RestController
@RequestMapping("/api/v1/contact")
public class ContactController {
    
    private static final Logger logger = LoggerFactory.getLogger(ContactController.class);
    
    private final ContactFormService contactFormService;
    private final ContactConfigService contactConfigService;
    
    public ContactController(ContactFormService contactFormService, ContactConfigService contactConfigService) {
        this.contactFormService = contactFormService;
        this.contactConfigService = contactConfigService;
    }
    
    /**
     * 提交联系表单
     */
    @PostMapping("/forms")
    public Result<ContactForm> submitContactForm(@RequestBody ContactFormDTO formDTO) {
        logger.info("接收到表单提交请求: {}", formDTO);
        
        try {
            // 手动验证必填字段
            if (!StringUtils.hasText(formDTO.getName())) {
                return Result.error(ResultCode.PARAM_ERROR.getCode(), "姓名不能为空");
            }
            if (!StringUtils.hasText(formDTO.getEmail())) {
                return Result.error(ResultCode.PARAM_ERROR.getCode(), "邮箱不能为空");
            }
            if (!StringUtils.hasText(formDTO.getMessage())) {
                return Result.error(ResultCode.PARAM_ERROR.getCode(), "咨询内容不能为空");
            }
            
            // 如果subject为空，设置默认值
            if (!StringUtils.hasText(formDTO.getSubject())) {
                formDTO.setSubject("网站咨询");
            }
            
            // 数据清洗
            formDTO.setName(formDTO.getName().trim());
            formDTO.setEmail(formDTO.getEmail().trim());
            if (formDTO.getCompany() != null) {
                formDTO.setCompany(formDTO.getCompany().trim());
            }
            if (formDTO.getPhone() != null) {
                formDTO.setPhone(formDTO.getPhone().trim());
            }
            formDTO.setSubject(formDTO.getSubject().trim());
            formDTO.setMessage(formDTO.getMessage().trim());
            
            ContactForm contactForm = contactFormService.submitContactForm(formDTO);
            logger.info("表单提交成功，ID: {}", contactForm.getId());
            return Result.success(contactForm);
        } catch (Exception e) {
            logger.error("表单提交失败", e);
            return Result.error(ResultCode.ERROR.getCode(), "提交失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取联系方式配置
     */
    @GetMapping("/configs")
    public Result<List<ContactConfig>> listContactConfigs() {
        try {
            List<ContactConfig> configs = contactConfigService.listContactConfigs();
            return Result.success(configs);
        } catch (Exception e) {
            logger.error("获取联系方式配置失败", e);
            return Result.error(ResultCode.ERROR.getCode(), "获取联系方式配置失败");
        }
    }
} 